package testhupai;

import java.util.HashMap;
import java.util.Map;

/**
 * 麻将类型枚举
 *
 * @author zkpursuit
 */
public enum CardType {

    wan(1, "万"), tiao(2, "条"), tong(3, "筒"),
    dong(40, "东风"), nan(41, "南风"), xi(42, "西风"),
    bei(43, "北风"), zhong(44, "中"), fa(45, "发"), ban(46, "白板");

    //类型
    private final int value;
    //牌名
    private final String name;

    private CardType(int value, String name) {
        this.value = value;
        this.name = name;
    }

    public int getValue() {
        return value;
    }

    public String getName() {
        return name;
    }

    private static final Map<Integer, String> numMap = new HashMap<>();
    private static final Map<Integer, CardType> types = new HashMap<>();
    private static final Map<Integer, String> typeNames = new HashMap<>();

    static {
        numMap.put(1, "一");
        numMap.put(2, "二");
        numMap.put(3, "三");
        numMap.put(4, "四");
        numMap.put(5, "五");
        numMap.put(6, "六");
        numMap.put(7, "七");
        numMap.put(8, "八");
        numMap.put(9, "九");
        CardType[] enums = CardType.values();
        for (CardType cardType : enums) {
            types.put(cardType.getValue(), cardType);
            typeNames.put(cardType.getValue(), cardType.getName());
        }
    }

    /**
     * 获取牌类型枚举
     *
     * @param typeValue 牌类型值
     * @return 牌类型枚举
     */
    public static final CardType getCardType(int typeValue) {
        return types.get(typeValue);
    }

    /**
     * 获取牌的类型名
     *
     * @param typeValue 牌类型
     * @return 牌类型名
     */
    public static final String getCardTypeName(int typeValue) {
        return typeNames.get(typeValue);
    }

    /**
     * 获取牌类型数值表示
     *
     * @param card 牌号
     * @return 牌类型数值表示
     */
    public static final int getCardTypeValue(int card) {
        if (card < 40) {
            return HandCards.getCardLeftValue(card);
        }
        return card;
    }

    /**
     * 将牌数据转换为现实中可读的牌
     *
     * @param card 牌数据
     * @return 现实中可读的牌
     */
    public static final String getCardName(int card) {
        if (card < 40) {
            int type = HandCards.getCardLeftValue(card);
            int point = HandCards.getCardRightValue(card);
            StringBuilder sb = new StringBuilder();
            sb.append(numMap.get(point));
            sb.append(getCardTypeName(type));
            return sb.toString();
        }
        return getCardTypeName(card);
    }

}
